package com.suzhe.ymall.promotion.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.suzhe.ymall.common.pojo.ApiResEntity;
import com.suzhe.ymall.common.pojo.AppLoginUser;
import com.suzhe.ymall.common.utils.LoginUserUtils;
import com.suzhe.ymall.promotion.enums.CouponStatusEnum;
import com.suzhe.ymall.promotion.param.LockCouponParam;
import com.suzhe.ymall.promotion.param.OrderCouponVoParam;
import com.suzhe.ymall.promotion.pojo.Coupon;
import com.suzhe.ymall.promotion.pojo.UserCoupon;
import com.suzhe.ymall.promotion.service.ICouponService;
import com.suzhe.ymall.promotion.service.IUserCouponService;
import com.suzhe.ymall.promotion.vo.UserCouponVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户优惠券表 前端控制器
 * </p>
 *
 * @author suzhe
 * @since 2019-08-18
 */
@RestController
@RequestMapping("/promotion")
public class PromotionController {

    @Autowired
    IUserCouponService iUserCouponService;

    @Autowired
    ICouponService iCouponService;

    @GetMapping("/getUserCouponVoInOrder")
    public List<UserCouponVo> getUserCouponVoInOrder( OrderCouponVoParam orderCouponVoParam) {

        AppLoginUser appLoginUser = LoginUserUtils.getAppLoginUser();
        List<UserCoupon> userCoupons = iUserCouponService.list(new QueryWrapper<UserCoupon>()//
                .eq("user_id", appLoginUser.getId())//
                .eq("status", CouponStatusEnum.AVAILABLE.value()));
        if (!userCoupons.isEmpty()) {
            List<String> couponIds = userCoupons.stream().map(item -> item.getCouponId()) //
                    .collect(Collectors.toList());
            List<Coupon> coupons = (List) iCouponService.listByIds(couponIds);
            Map<String, Coupon> couponMap = coupons.stream().//
                    collect(Collectors.toMap(item -> item.getId(), item -> item));
            List<UserCouponVo> couponVos = userCoupons.stream().map(item -> {
                UserCouponVo userCouponVo = new UserCouponVo();
                userCouponVo.setUserCouponId(item.getId());
                userCouponVo.setStartTime(item.getStartTime());
                userCouponVo.setEndTime(item.getEndTime());

                Coupon coupon = couponMap.get(item.getCouponId());
                userCouponVo.setValue(coupon.getCouponValue());
                userCouponVo.setCouponType(coupon.getCouponType());
                userCouponVo.setName(coupon.getName());
                userCouponVo.setCondition(coupon.getDescription());
                return userCouponVo;
            }).collect(Collectors.toList());
            return couponVos;
        }
        return Collections.emptyList();
    }

    @PostMapping("/lockUserCoupon")
    public ApiResEntity lockUserCoupon(@RequestBody LockCouponParam lockCouponParam) {
        ApiResEntity apiResEntity = iUserCouponService.lockUserCoupon(lockCouponParam);
        return apiResEntity;
    }

    @PostMapping("/orderSuccess")
    public ApiResEntity orderSuccess(@RequestBody Long orderNo){
        UserCoupon userCoupon = iUserCouponService.getOne(new QueryWrapper<UserCoupon>().eq("order_no", orderNo));
        userCoupon.setStatus(CouponStatusEnum.USED.value());
        iUserCouponService.updateById(userCoupon);
        return ApiResEntity.ok();
    }
}
